//
//  ScroTitleView.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/30.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ScroTitleView.h"

@interface ScroTitleView ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView  * tableView;
@property(nonatomic,assign)NSInteger scroIndex;
@property(nonatomic,strong)NSArray * titleArray;
@property(nonatomic,strong)void(^complect)(NSInteger index);

@property(nonatomic,strong)UILabel * currentLabel;
@property(nonatomic,assign)NSInteger currentIndex;
@end

@implementation ScroTitleView
- (instancetype)initWithFrame:(CGRect)frame Titles:(NSArray * )titles complect:(void(^)(NSInteger index))complect{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _complect = complect;
        _tableView = [[UITableView alloc]initWithFrame:self.frame style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _titleArray = titles;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.scrollEnabled = NO;
        _complect(0);
        [self addSubview:_tableView];
    }
    
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame TitleArray:(NSArray * )titleArray complect:(void(^)(NSInteger index))complect{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _complect = complect;
        _currentIndex =0;
        _titleArray = titleArray;
        
    
        _currentLabel = [[UILabel alloc]initWithFrame:self.frame];
        _currentLabel.text = _titleArray[_currentIndex];
        _currentLabel.textAlignment = NSTextAlignmentCenter;
        _currentLabel.backgroundColor = [UIColor clearColor];
        _currentLabel.textColor = [UIColor colorWithWhite:0.427 alpha:1.000];
        _currentLabel.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];

        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.frame = self.frame;
        [button addTarget:self action:@selector(changeIndex:) forControlEvents:UIControlEventTouchUpInside];
        
        
        if (_titleArray.count == 0) {
            NSLog(@"标题不能少于1");
        }else if(_titleArray.count == 1){
            _currentLabel.text = _titleArray[0];
        }else{
            _currentLabel.text = _titleArray[0];
        }
        
        
        [self addSubview:self.currentLabel];
        [self addSubview:button];
    }

    return self;
}

- (void)changeIndex:(UIButton *)sender{
    if (_titleArray.count == 0) {
        
    }else if (_titleArray.count == 1){
        _currentIndex = 0;
    }else{
  
        if (_currentIndex==_titleArray.count-1) {
            _currentIndex=0;
        }else{
          _currentIndex += 1;
        }
    }
    
    _complect(_currentIndex);

        _currentLabel.alpha = 0;
        [UIView animateWithDuration:0.01 animations:^{

        _currentLabel.text = _titleArray[_currentIndex];
            _currentLabel.frame = flexibleFrame(CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height), NO);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                 _currentLabel.frame = self.frame;
            }];
        }];
    

}


#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35*VerticalRatio();
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count*20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIdentify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    cell.backgroundColor = [UIColor clearColor];
    UILabel * title = [[UILabel alloc]initWithFrame:self.frame];
    title.text = _titleArray[indexPath.row%(_titleArray.count)];
    title.textAlignment = NSTextAlignmentCenter;
    title.backgroundColor = [UIColor clearColor];
    title.textColor = [UIColor colorWithWhite:0.427 alpha:1.000];
    title.font = [UIFont systemFontOfSize:14*VerticalTexeRatio()];
    [cell addSubview:title];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row<_titleArray.count*20-1) {
        [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }else{
    [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
    
    _complect((indexPath.row+1)%_titleArray.count);
}



- (UILabel *)currentLabel{
    if (!_currentLabel) {
        _currentLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:self.frame];
            label.text = _titleArray[_currentIndex];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor colorWithWhite:0.427 alpha:1.000];
            label.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];
            label;
        });
    }
    return _currentLabel;
}

@end
